<!-- UserList 管理员用户列表 -->
<template>
  <div class="content">
    <el-form :model="query" ref="form" size="medium" class="search-form">
      <el-form-item>
        <el-input v-model="query.keyword" placeholder="请输入用户名/角色/手机号" @keyup.enter.native="handleSearch"></el-input>
      </el-form-item>
      <el-form-item v-if="showShopIdQuery">
        <StoreSelect v-model="query.shopId" filterable></StoreSelect>
      </el-form-item>
      <el-form-item label-width="10px">
        <Cbutton @click="handleSearch" :btnloading="tableLoading">查询</Cbutton>
      </el-form-item>
    </el-form>

    <div class="content-container">
      <div class="content-btn">
        <Cbutton @click="handleAdd">创建账号</Cbutton>
      </div>
      <el-table :data="tableList" v-loading="tableLoading" ref="tableData" border fit>
        <el-table-column label="序号" type="index" align="center" width="70"></el-table-column>
        <!-- <el-table-column prop="username" label="登陆用户名" align="center" min-width="120"></el-table-column> -->
        <el-table-column prop="personName" label="用户名" align="center" min-width="100"></el-table-column>
        <el-table-column prop="shopName" label="关联店铺" align="center" min-width="100"></el-table-column>
        <el-table-column prop="roleName" label="角色" align="center"></el-table-column>
        <el-table-column prop="roleAdmin" label="权限" align="center">
          <template slot-scope="scope">
            {{scope.row.roleAdmin === 1 ? '管理员' : '普通用户'}}
          </template>
        </el-table-column>
        <el-table-column prop="personMobile" label="手机号" align="center" min-width="90"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center" min-width="140">
          <template slot-scope="scope">
            <span>{{scope.row.createTime | glbTimeFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="loginTime" label="最近登录时间" align="center" min-width="140">
          <template slot-scope="scope">
            <span>{{scope.row.loginTime | glbTimeFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            {{ dealListStr(scope.row.status, 'userStatusList') }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" min-width="120" align="center">
          <template slot-scope="scope">
            <Cbutton type="text" size="medium" @click="handleEdit(scope.row)">修改</Cbutton>
            <el-button v-if="scope.row.status === 0" type="text" size="medium" @click="hanldleEnableUser(scope.row)">启用</el-button>
            <el-button v-if="scope.row.status === 1" type="text" size="medium" style="color: #F56C6C" @click="hanldleDisableUser(scope.row)">禁用</el-button>
            <Cbutton type="text" size="medium" color="danger" @click="handleRemove(scope.row)">删除</Cbutton>
          </template>
        </el-table-column>
      </el-table>

      <Pagination @pageChange="getPage" :pageDefultData="{
        pageNum: query.pageNum,
        pageRow: query.pageSize,
        totalCount: totalCount }">
      </Pagination>
    </div>

    <!-- 创建账号 修改信息 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.mVisible" v-if="dialog.mVisible" width="500px" class="dialog-form">
      <el-form :model="dialog.data" :rules="dialog.rules" ref="DlalogForm" size="medium" label-width="100px">
        <el-form-item label="用户名：" prop="personName">
          <el-input v-model="dialog.data.personName" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="personMobile">
          <el-input v-model="dialog.data.personMobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="角色：" prop="roleId">
          <RoleSelect v-model="dialog.data.roleId"></RoleSelect>
        </el-form-item>
        <el-form-item label="店铺归属：" prop="shopId">
          <StoreSelect v-model="dialog.data.shopId" filterable></StoreSelect>
        </el-form-item>
        <!-- <el-form-item label="数据权限：" prop="roleAdmin">
          <Cselect v-model="query.roleAdmin" :options="permissionsList" placeholder="请选择权限"></Cselect>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialog.mVisible = false">取 消</el-button>
        <el-button size="medium" :loading="dialog.loading" type="primary" @click="handleDiaSub('DlalogForm')">确
          定</el-button>
      </span>
    </el-dialog>
    <!-- 创建账号 修改信息 -->

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { ExportList, ExportStr } from '@/utils'
import { UserInfoRules } from '@/utils/rules'
import { GetUserList, AddUser, EditUser, DisableUser, EnableUser, RemoveUser } from '@/api/system'
import RoleSelect from '@/components/Form/Select/roleSelect'
import StoreSelect from '@/components/Form/Select/storeSelect'
export default {
  name: 'UserList',
  components: { RoleSelect, StoreSelect },
  data () {
    return {
      showShopIdQuery: false,
      query: {
        keyword: '',
        shopId: '',
        pageNum: 1,
        pageSize: 10
      },
      totalCount: 0,
      tableList: [],
      tableSelectList: [],
      tableLoading: false,
      exportLoading: false,
      userStatusList: ExportList('userStausList'),
      permissionsList: [
        { value: '1', label: '管理员权限' },
        { value: '0', label: '普通用户权限' }
      ],
      dialog: {
        title: '',
        mVisible: false,
        loading: false,
        oprType: 'ADD',
        data: {
          personName: '',
          personMobile: '',
          roleId: '',
          shopId: '',
          roleAdmin: '1',
          campusId: '2'
        },
        rules: UserInfoRules
      }
    }
  },
  props: {},
  watch: {
    'userShopId' (val) {
      this.showShopIdQuery = val === 0 || val === '0'
    }
  },
  computed: {
    ...mapGetters([
      'userShopId'
    ])
  },
  created () {
    this.$store.dispatch('getLoginUserInfo')
  },
  mounted () {
    this.getList()
  },
  methods: {
    getList () {
      this.tableLoading = true
      let params = JSON.parse(JSON.stringify(this.query))
      GetUserList(params).then(res => {
        this.tableLoading = false
        const data = res.data
        this.tableList = data.list
        this.totalCount = data.total
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleSearch () {
      this.query.pageNum = 1
      this.getList()
    },
    handleEdit (row) {
      this.dialog.title = '修改'
      this.dialog.mVisible = true
      this.dialog.oprType = 'EDIT'
      this.dialog.data.id = row.id
      for (const key in this.dialog.data) {
        if (key === 'roleId') {
          this.dialog.data.roleId = row.roleId.toString()
        } else if (key === 'shopId') {
          this.dialog.data.shopId = row.shopId === 0 || row.shopId ? row.shopId.toString() : ''
        } else {
          this.dialog.data[key] = row[key]
        }
      }
    },
    handleAdd () {
      this.dialog.title = '创建'
      this.dialog.mVisible = true
      this.dialog.oprType = 'ADD'
      for (const key in this.dialog.data) {
        this.dialog.data[key] = ''
      }
      if (this.dialog.data.id) {
        delete this.dialog.data.id
      }
      this.dialog.data.campusId = '2'
      this.dialog.data.roleAdmin = '1'
    },
    handleDiaSub (formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return false
        this.dialog.loading = true
        if (this.dialog.oprType === 'ADD') {
          const parmas = JSON.parse(JSON.stringify(this.dialog.data))
          AddUser(parmas).then(res => {
            this.$message.success(res.message || '操作成功')
            this.getList()
            this.dialog.loading = false
            this.dialog.mVisible = false
            this.$store.dispatch('getLoginUserInfo')
          }).catch(() => {
            this.dialog.loading = false
          })
        }
        if (this.dialog.oprType === 'EDIT') {
          const parmas = JSON.parse(JSON.stringify(this.dialog.data))
          EditUser(parmas).then(res => {
            this.$message.success(res.message || '操作成功')
            this.getList()
            this.dialog.loading = false
            this.dialog.mVisible = false
            this.$store.dispatch('getLoginUserInfo')
          }).catch(() => {
            this.dialog.loading = false
          })
        }
      })
    },
    // 启用
    hanldleEnableUser (row) {
      this.$confirm(`确定要启用该用户吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        EnableUser({ id: row.id }).then(res => {
          this.getList()
        }).catch(() => { })
      }).catch(() => { })
    },
    // 禁用
    hanldleDisableUser (row) {
      this.$confirm(`确定要禁用该用户吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DisableUser({ id: row.id }).then(res => {
          this.getList()
        }).catch(() => { })
      }).catch(() => { })
    },
    // 删除
    handleRemove (row) {
      this.$confirm(`确定要删除该用户吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        RemoveUser({ id: row.id }).then(res => {
          this.getList()
          this.$message.success(res.message || '操作成功')
        }).catch(() => { })
      }).catch(() => { })
    },
    getPage (val) {
      this.query.pageNum = val.pageNum
      this.query.pageSize = val.pageRow
      this.getList()
    },
    dealListStr (key, type) {
      return ExportStr(key, type)
    }

  }
}

</script>
<style lang='scss' scoped>
</style>
